package com.songcha.sakura.ui.adapter

import android.content.Context
import android.graphics.Bitmap
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.RequestOptions


import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.sakura.R
import com.songcha.sakura.struct.M3u8LocalFile
import com.songcha.sakura.ui.decoration.LocalVideoItemDecoration
import java.security.MessageDigest

/*enum class PayLoadLocalDownload{
    THUMB,
    VIDEOS
}*/


class LocalDownloadAdapter(val mContext: Context,resId:Int,list:MutableList<M3u8LocalFile>,val mListener:LocalVideoAdapter.IOnLocalVideoListener) : BaseQuickAdapter<M3u8LocalFile,BaseViewHolder>(resId,list) {

    //private var mAdapter:LocalVideoAdapter?=null
    private val PAYLOAD_THUMB="thumb"
    private val PAYLOAD_VIDEOS="video"
    private var mLocalList= mutableListOf<M3u8LocalFile.LocalVideo>()

    fun updateVideoCurrentSize(i:Int,j:Int){
        val vh=recyclerView.findViewHolderForAdapterPosition(i)?:return
        vh as BaseViewHolder
        val rcvVideo=vh.getView<RecyclerView>(R.id.rcv_video)
        rcvVideo.adapter?.notifyItemChanged(j, LocalVideoAdapter.PayLoadLocalVideo.FILE_SIZE)
    }

    override fun convert(holder: BaseViewHolder, item:M3u8LocalFile,payloads:List<Any>) {
        if(payloads.isEmpty()){
            super.convert(holder, item, payloads)
            return
        }

        val payload=payloads[0]
        payload as HashMap<String,Any?>

        for(key in payload.keys){
            when(key){
                PAYLOAD_THUMB->{
                    Glide.with(mContext).load(item.absolutePath+"/thumb.jpg")
                        .apply(RequestOptions().transform(object :BitmapTransformation(){
                            override fun updateDiskCacheKey(messageDigest: MessageDigest) {

                            }

                            override fun transform(
                                pool: BitmapPool,
                                toTransform: Bitmap,
                                outWidth: Int,
                                outHeight: Int
                            ): Bitmap {
                                return toTransform
                            }

                        }))
                        .into(holder.getView(R.id.iv_thumb))

                    holder.getView<TextView>(R.id.tv_title).setText(item.title)

                }
                PAYLOAD_VIDEOS->{
                    val rcvVideo=holder.getView<RecyclerView>(R.id.rcv_video)
                    mLocalList=item.list

                    //嵌套adapter无法局部刷新

                    rcvVideo.apply {
                        layoutManager=LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false)
                        if(itemDecorationCount==0)
                            addItemDecoration(LocalVideoItemDecoration())

                        adapter=LocalVideoAdapter(context, mLocalList,mListener)

                    }


                }
            }


        }

    }

    override fun convert(holder: BaseViewHolder, item:M3u8LocalFile) {
        val payload=HashMap<String,Any?>()
        payload[PAYLOAD_THUMB] = null
        payload[PAYLOAD_VIDEOS] = null
        convert(holder,item, arrayListOf(payload))
    }


   /* fun updateVideo(i:Int,j:Int,localVideo:M3u8LocalFile.LocalVideo){
        mIndex=j
        val payload=HashMap<String,Any?>()
        payload[PAYLOAD_THUMB] = null
        payload[PAYLOAD_VIDEOS] = localVideo
        notifyItemChanged(i,payload)
    }*/


}